package com.example.tut22;

import android.os.BatteryManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewStub;
import android.widget.TextView;

public class Tut22 extends Activity {

	private float battery=-1;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tut22);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_tut22, menu);
        return true;
        
    }
    
    
   protected void onResume() {
          
        super.onResume();
 
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        filter.addAction(Intent.ACTION_BATTERY_LOW);
        // filter.addAction(Intent.ACTION_BATTERY_CRITICAL);
        filter.addAction(Intent.ACTION_BATTERY_OKAY);
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        registerReceiver(mIntentReceiver, filter);
    }
   
   protected void onPause() {
      // Log.i(TAG, "onPause()");
	   //Don't think this is what we ant as we won't receive battery broadcast messages when paused
	   //not really sure what paused is and without unregistering it, it throws an exception. This can
	   //be seen when rotating tablet, side note rotating tablet cause string to default
        
       super.onPause();
        
       unregisterReceiver(mIntentReceiver);
       //((TextView)findViewById(R.id.txtBattery)).setText(Float.toString(this.battery));
   }

   public void buttonOnClick(View v) {
	  //System.err.println("test2");
	  // System.out.println("test");
	  // R.id.txtBattery.setText("90%");
	   ((TextView)findViewById(R.id.txtBattery)).setText(Float.toString(this.battery));
   }
   
   
   private void updateBatteryStatus(Intent intent){
	   
	   int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
	   int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

	   this.battery = level / (float)scale *100;
	  // String val = intent.getStringExtra("battery_level");
	  // val=Float.toString(this.battery);
	 //  R.id.txtBattery
	   System.out.println("Got Battery " + Float.toString(this.battery));
   }
   
   private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
           String action = intent.getAction();
    
          if (action.equals(Intent.ACTION_BATTERY_CHANGED))
               updateBatteryStatus(intent);
           else if (action.equals(Intent.ACTION_BATTERY_LOW) ||
                   // action.equals(Intent.ACTION_BATTERY_CRITICAL) ||
                   action.equals(Intent.ACTION_BATTERY_OKAY)){
        	   			//updateWarning(intent);
           }
           else if (action.equals(Intent.ACTION_POWER_CONNECTED) ||
                   action.equals(Intent.ACTION_POWER_DISCONNECTED)){
               			//updatePlugged(intent);
           }
       }
   };
}
